package com.example.backgroundmanger.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.backgroundmanger.commons.BusinessException;
import com.example.backgroundmanger.commons.JwtUtil;
import com.example.backgroundmanger.entity.User;
import com.example.backgroundmanger.mapper.UserMapper;
import com.example.backgroundmanger.service.IUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 坤哥
 * @since 2024-07-01
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {
   //注入持久层
    @Resource
    private UserMapper userMapper;
    //注入JWTUtil
    @Resource
    private JwtUtil jwtUtil;

    @Override
    public boolean userRegisterService(User user) {
        // 参数校验
        if (user == null || user.getUsername() == null || user.getUsername().trim().isEmpty()) {
           throw new BusinessException(409,"用户信息不完整");
        }

        //1.判断用户名是否存在
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(User::getUsername, user.getUsername());
        User existingUser = userMapper.selectOne(wrapper);
        
        if (existingUser != null) {
            // 用户名存在
            throw new BusinessException(409,"用户名已存在");
        }
        
        // 执行插入操作
        int insert = userMapper.insert(user);
        return insert > 0;  // 如果大于0,注册成功
    }

    @Override
    public Map<String, Object> userLoginService(String username, String password) {
        Map<String, Object> map = new HashMap<>();
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(User::getUsername, username).eq(User::getPassword, password);
        User loginUser = userMapper.selectOne(wrapper);
        if (loginUser == null) {
            // 登录失败
            throw new BusinessException(400,"账号或密码错误");
        } else {
            // 登录成功
            // 生成Token(令牌)
            loginUser.setPassword(null);
            String token = jwtUtil.createToken(loginUser);
            map.put("token", token);
            map.put("user", loginUser);
            return map;
        }
    }

    // 登录初始化
    @Override
    public Map<String, Object> userInfoService(String token) {
        try {
            Map<String, Object> map = new HashMap<>();
            // 解析token
            User user = jwtUtil.parseToken(token, User.class);
            map.put("id", user.getId());
            map.put("avatar", user.getAvatar());
            map.put("name", user.getUsername());
            map.put("roles", user.getRole());
            map.put("phone", user.getPhone());
            map.put("email", user.getEmail());
            return map;
        } catch (Exception e) {
            throw new BusinessException(400,"token解析错误");
        }
    }
}
